//
//  BAHeaderNode.m
//  BabyAlbum
//
//  Created by master on 17/2/15.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAVideoHeaderNode.h"
#import "BAResource.h"
@interface BAVideoHeaderNode () <ASEditableTextNodeDelegate,ASVideoNodeDelegate>

@property (nonatomic, strong) ASVideoNode *coverVideoNode;

@property (nonatomic, strong) ASTextNode *dateNode;
@property (nonatomic, strong) ASDisplayNode *lineNode;
@property (nonatomic, strong) ASTextNode *detailNode;
@property (nonatomic, strong) BAAlbumModel *model;
@property (nonatomic, strong) ASButtonNode *playButton;
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASTextNode *weekNode;
//@property (nonatomic, assign) BOOL isEditing;
//@property (nonatomic, copy) void (^selectBlock)(void) ;
//@property (nonatomic, copy) void (^textChangeBlock)(NSString *text) ;


@end

@implementation BAVideoHeaderNode

- (BOOL)videoNode:(ASVideoNode*)videoNode shouldChangePlayerStateTo:(ASVideoNodePlayerState)state {
    if(state == ASVideoNodePlayerStatePlaying) {
        _playButton.hidden = YES;
    } else {
        _playButton.hidden = NO;
    }
    return YES;
}

- (instancetype)initWithData:(BAAlbumModel *)model;
{
    if(self = [super init]) {

        self.model = model;
//        _isEditing = isEdit;
        BAAlbumVideoItem *video = (BAAlbumVideoItem *)model.cover;
        _coverVideoNode = [[ASVideoNode alloc] init];
        _coverVideoNode.backgroundColor = [UIColor whiteColor];
        //        NSURL *url = [NSURL fileURLWithPath:model.coverVideo.URL.absoluteString];
        
        _coverVideoNode.asset = video.video;//[AVAsset assetWithURL:url];
        _coverVideoNode.gravity = AVLayerVideoGravityResizeAspect;
        _coverVideoNode.backgroundColor = [UIColor clearColor];
        _coverVideoNode.shouldAutorepeat = YES;
        _coverVideoNode.shouldAutoplay = NO;
        _coverVideoNode.delegate = self;
//        _coverVideoNode.userInteractionEnabled = isEdit;
        _coverVideoNode.muted = YES;
        [self addSubnode:_coverVideoNode];
        
        _playButton = [[ASButtonNode alloc] init];
        _playButton.imageNode.contentMode = UIViewContentModeScaleAspectFit;
        [_playButton setImage:[UIImage imageNamed:@"play_status"] forState:ASControlStateNormal];
        [_coverVideoNode addSubnode:_playButton];
        
        
        
        _titleNode = [[ASTextNode alloc] init];
        NSAttributedString *string =[NSAttributedString attributedStringWithString:model.title fontSize:18 color:RGBWhite(44)];
        _titleNode.attributedText = string;
        [self addSubnode:_titleNode];
        
        
        _dateNode = [[ASTextNode alloc] init];
        string =[NSAttributedString attributedStringWithString:[model customDateShow] fontSize:13 color:RGB_255(188,197,195)];
        _dateNode.attributedText = string;
        [self addSubnode:_dateNode];
        
        
        _weekNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:[[model weekDayShow] length]?[model weekDayShow]:[[NSDate date] weekDayShow] fontSize:13 color:RGB_255(188,197,195)];
        _weekNode.attributedText = string;
        [self addSubnode:_weekNode];
        
        
        _detailNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:model.desc fontSize:11 color:RGBWhite(140)];
        _detailNode.attributedText = string;
        [self addSubnode:_detailNode];


        [self addSubnode:_detailNode];
        
    }
    return self;
}




- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return NO;
}


- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    //    _coverNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.width);
    _coverVideoNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.width);
    
    _dateNode.style.flexShrink  = YES;
    _lineNode.style.preferredSize = CGSizeMake(kScreenWidth - 40, 1);
    _detailNode.style.flexShrink = YES;
    _detailNode.style.minSize = CGSizeMake(kScreenWidth - 20, 80);
//    _playButton.style.preferredSize  = CGSizeMake(30, 30);

    _playButton.frame = CGRectMake(constrainedSize.max.width - 30 - 10, constrainedSize.max.width - 30 - 10, 30, 30);
    _coverVideoNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.width);
    _dateNode.style.flexShrink  = YES;
    _titleNode.style.flexShrink  = NO;
    //    _titleNode.style.maxSize = CGSizeMake(constrainedSize.max.width, 30);
    _dateNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 60, 20);
    
    _detailNode.style.flexShrink = YES;
    _detailNode.style.minSize = CGSizeMake(kScreenWidth - 20, 80);
    
    
    ASRelativeLayoutSpec *dateSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_dateNode];
    ASRelativeLayoutSpec *weekSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionEnd verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_weekNode];
    
    ASStackLayoutSpec *dataHor = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsCenter children:@[dateSpec,weekSpec]];
    
    
    ASStackLayoutSpec *ver1 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_coverVideoNode,_titleNode,dataHor,_detailNode]];
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:ver1];
    
    
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:insetLayout];
}

+ (CGFloat)calculteHeightWithData:(BAAlbumModel *)albumModel {
//    CGFloat descHeight = [albumModel.desc uxy_calculateSize:CGSizeMake(kScreenWidth - 30, CGFLOAT_MAX) font:[UIFont systemFontOfSize:11]].height;
    CGFloat descHeight = [UILabel getHeightByWidth:kScreenWidth - 20 title:albumModel.desc font:[UIFont systemFontOfSize:11]];
    CGFloat titleHeight = [UILabel getHeightByWidth:kScreenWidth - 20 title:albumModel.title font:[UIFont systemFontOfSize:18]];
    
    if(descHeight < 10) {
        descHeight = 80;
    }
    CGFloat height = 5 + kScreenWidth - 20 + 5 + 5 + descHeight + 5 + titleHeight + 5+ 10;
    return height;
//    CGFloat descHeight = [UILabel getHeightByWidth:kScreenWidth - 30 title:albumModel.desc font:[UIFont systemFontOfSize:11]];
//
//    if(descHeight < 10) {
//        descHeight = 40;
//    }
//    CGFloat height = 5 + kScreenWidth - 20 + 5 + 13.5 + 5 + 1+ 5 + descHeight + 20+ 20;
//    return height;
    
}

#pragma show and hide the keyboard
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}
@end
